package cn.czh.controller;

import cn.czh.domain.User;
import cn.czh.downgrade.DowngradeUtil;
import cn.czh.downgrade.FallbackMethodUtil;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import jdk.nashorn.internal.ir.annotations.Reference;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RefreshScope // 动态刷新
public class UserController {

    @Value("${dynamicRefreshTest}")
    private String dynamicRefreshTest;

    /**
     * 限流服务降级
     * @param id
     * @return
     */
   /* @GetMapping("/getById/{id}")
    @SentinelResource(value = "getById", blockHandler = "blockMethod",blockHandlerClass = DowngradeUtil.class)
    public User getById(@PathVariable("id") Long id) {
        return new User(id, "源码时代", "在源码时代学Java" + ";" + "dynamicRefreshTest:" + dynamicRefreshTest);
    }*/

   /* // 本类服务降级方法
    public User blockMethod(@PathVariable("id") Long id, BlockException e) {
        e.printStackTrace();
        return new User(-1L,"服务降级触发","当前服务流量过大,请稍后重试");
    }*/

    @GetMapping("/getById/{id}")
    @SentinelResource(value = "getById", fallback = "fallbackMethod",fallbackClass = FallbackMethodUtil.class)
    public User getById(@PathVariable("id") Long id) {
        try {
            // 用于让服务慢响应
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return new User(id, "源码时代", "在源码时代学Java" + ";" + "dynamicRefreshTest:" + dynamicRefreshTest);
    }

   /* public User fallbackMethod(@PathVariable("id") Long id, Throwable e) {
        e.printStackTrace();
        return new User(-1L,"熔断服务降级触发","服务不可用,请稍后重试");
    }*/







}

